package cn.stylefeng.guns.modular.business.frequency.orderpayrecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.orderpayrecord.entity.OrderPayRecord;
import cn.stylefeng.guns.modular.business.frequency.orderpayrecord.mapper.OrderPayRecordMapper;
import cn.stylefeng.guns.modular.business.frequency.orderpayrecord.model.param.OrderPayRecordParam;
import cn.stylefeng.guns.modular.business.frequency.orderpayrecord.service.OrderPayRecordService;
import cn.stylefeng.guns.modular.business.frequency.orderpayrecord.enums.OrderPayRecordExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * tt_order_pay_record 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderPayRecordServiceImpl extends ServiceImpl<OrderPayRecordMapper, OrderPayRecord> implements OrderPayRecordService {

    @Override
    public PageResult<OrderPayRecord> page(OrderPayRecordParam orderPayRecordParam) {

        // 构造条件
        LambdaQueryWrapper<OrderPayRecord> queryWrapper = new LambdaQueryWrapper<>();


        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderPayRecord> list(OrderPayRecordParam orderPayRecordParam) {

        // 构造条件
        LambdaQueryWrapper<OrderPayRecord> queryWrapper = new LambdaQueryWrapper<>();


        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderPayRecordParam orderPayRecordParam) {

        // 将dto转为实体
        OrderPayRecord orderPayRecord = new OrderPayRecord();
        BeanUtil.copyProperties(orderPayRecordParam, orderPayRecord);


        this.save(orderPayRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderPayRecordParam orderPayRecordParam) {

        // 根据id查询实体
        OrderPayRecord orderPayRecord = this.queryOrderPayRecord(orderPayRecordParam);


        // 更新实体
        this.updateById(orderPayRecord);

    }

    @Override
    public void edit(OrderPayRecordParam orderPayRecordParam) {

        // 根据id查询实体
        OrderPayRecord orderPayRecord = this.queryOrderPayRecord(orderPayRecordParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderPayRecordParam, orderPayRecord);


        // 不能修改状态，用修改状态接口修改状态
        orderPayRecord.setStatus(null);

        this.updateById(orderPayRecord);
    }

    @Override
    public OrderPayRecord detail(OrderPayRecordParam orderPayRecordParam) {
        return this.queryOrderPayRecord(orderPayRecordParam);
    }

    @Override
    public void changeStatus(OrderPayRecordParam orderPayRecordParam) {
        Long id = orderPayRecordParam.getId();
        String status = orderPayRecordParam.getStatus();



        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<OrderPayRecord> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
            .eq(OrderPayRecord::getId, id).set(OrderPayRecord::getStatus, status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }


    /**
     * 获取tt_order_pay_record
     *
     * @author yujie
     * @since 2023/05/09 17:16
     */
    private OrderPayRecord queryOrderPayRecord(OrderPayRecordParam orderPayRecordParam) {
        OrderPayRecord orderPayRecord = this.getById(orderPayRecordParam.getId());
        if (ObjectUtil.isEmpty(orderPayRecord)) {
            throw new ServiceException(OrderPayRecordExceptionEnum.NOT_EXISTED);
        }
        return orderPayRecord;
    }
}